<?php

namespace Fluency\Web\Http;

use Fluency\Web\RespondingPolicy;

/**
 * A derivation of the Request class representing a request that is being dispatched by a
 * RequestDispatcher. This class includes a reference to the RespondingPolicy, so it is mutable,
 * unlike the base Request class.
 *
 * @package Fluency\Web\Http
 * @author  Ignas Rudaitis
 * @since   1.0
 */
class ActiveRequest extends Request
{
    private $policy;

    /**
     * The constructor. It takes a Request object to copy basic request data from and a
     * RespondingPolicy to be associated with the ActiveRequest.
     *
     * @param Fluency\Web\Http\Request $req
     * @param Fluency\Web\RespondingPolicy $policy
     */
    public function __construct(Request $req, RespondingPolicy $policy)
    {
        parent::__construct($req->getDispatcher(), $req);
        $this->policy = $policy;
    }

    /**
     * Returns the responding policy associated with the request.
     *
     * @return Fluency\Web\RespondingPolicy
     */
    public function getRespondingPolicy()
    {
        return $this->policy;
    }

    /**
     * Renders a given value using the renderer identified by the given identifier and returns the
     * rendering result.
     *
     * @param string $rendererId the renderer identifier
     * @param mixed $input the value to render
     * @return mixed
     */
    public function render($rendererId, $input)
    {
        $renderer = $this->getDispatcher()->getRenderingFilter()->makeRenderer($rendererId, $this);
        return $renderer($input);
    }
}
